﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;

using Module;
using System.Collections.Specialized;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        SysReg.aspx.cs           
/// Author :         Wangbc
/// CDT:             2011-12-13           
/// Version:         1.0.0.0                
/// Depiction:       系统信息注册    
/// </summary>
public partial class main_admin_SysReg :  System.Web.UI.Page
{
    /// <summary>
    /// 系统信息EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 系统信息Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    /// 系统信息Entity名字
    /// </summary>    
    private const string EM_FULL_NAME = WEBConstants.EM_SYS_MANAGE;

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //系统信息ID
        string chkID = "1";
        //系统CODE不作处理
        tr_SYS_CODE.Controls.Clear();

        em = CDEEMFactory.getEMByFullName(EM_FULL_NAME);
        if (!Page.IsPostBack)
        {
            //ID不为空修改系统信息，否则新增系统信息
            if (chkID != "" && chkID != null)
            {
                //修改
                this.page_title.InnerText = "系统信息注册";
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("ID", chkID);
                List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
                //系统信息ID是否存在
                if (listEntity.Count > 0)
                {                    
                    m_entity = listEntity[0];
                    m_entity.Operation = CDEDataOperation.UPDATE;

                    string SYS_CODE = Util.objectToString(m_entity.get("SYS_CODE"));
                    if (SYS_CODE != "") {
                       // Response.Redirect("~/Login.aspx");   
                    }
                }
                else
                {
                    m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                    m_entity.Operation = CDEDataOperation.INSERT;
                }
            }
           
            Session["MENTITY"] = m_entity;
                     
        }
        else
        {
            m_entity = (CDEEntity)Session["MENTITY"];
            //页面信息放入系统信息Entity
            this.setEntityFromRequest(m_entity, Request.Params);
        }
    }
    /// <summary>
    /// 确定按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void queding_Click(object sender, EventArgs e)
    {
        List<CDEEntity> listEntity = new List<CDEEntity>();

        listEntity.Add(m_entity);

        bool isSaveError = false;

        //系统代码初始设置
        string guid = Guid.NewGuid().ToString().ToUpper();
        m_entity.set("SYS_CODE",guid);
             
        CDESearchCriteria vsc = new CDESearchCriteria(em);
        CDEService.UpdateEntities(em, listEntity);

        if (listEntity[0].Result == CDEDataOperationResult.ERROR)
        {
            isSaveError = true;
        }
        else
        {
            isSaveError = false;
        }

        //成功返回列表，失败提示错误信息
        if (isSaveError)
        {
            showMessageErrAndOk(Resources.ResourceErrMsg.SAVE_ERR);
            
        }
        else
        {

            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.AppendFormat("alert(\"");
            sbr.AppendFormat("注册成功!");
           // sbr.AppendFormat("\");</script>");
            sbr.AppendFormat("\");window.location='../../Login.aspx';</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
        }

    }

    /// <summary>
    /// 提示信息
    /// </summary>
    /// <param name="strMsg"></param>
    protected void showMessageErrAndOk(string strMsg)
    {
        StringBuilder sbr = new StringBuilder();
        sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
        sbr.AppendFormat("alert(\"");
        sbr.AppendFormat(strMsg);
        sbr.AppendFormat("\");</script>{0}", Environment.NewLine);
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "PageStartup", sbr.ToString());
    }

    /// <summary>
    /// 取页面值构造Entity
    /// </summary>
    /// <param name="entity"></param>
    /// <param name="request"></param>
    protected void setEntityFromRequest(CDEEntity entity, NameValueCollection request)
    {
        if (entity == null)
        {
            return;
        }
        string control_id = string.Empty;

        foreach (CDEAttribute attribute in entity.EntityManager.ListAttributes)
        {
            if (CDEConstants.ConstDispType_NO_DISP.Equals(attribute.DISP_type))
            {
                continue;
            }

            else if (CDEConstants.ConstPointType_P_SOURCE.Equals(attribute.P_type))
            {
                control_id = "hid_" + attribute.Name;
            }
            else if (CDEConstants.ConstPointType_P_TARGET.Equals(attribute.P_type))
            {
                control_id = "ddl_" + attribute.Name;
                if (this.FindControl(control_id) == null)
                {
                    control_id = "txt_" + attribute.Name;
                }
            }

            else if (attribute.getP_Colmun().isHaveCandidate())
            {
                control_id = "ddl_" + attribute.Name;
            }

            //string
            else if (ConstDataType.STRING.Equals(attribute.getP_Colmun().Type))
            {
                control_id = "txt_" + attribute.Name;
            }
            //Number
            else if (ConstDataType.NUMBER.Equals(attribute.getP_Colmun().Type))
            {
                control_id = "txt_" + attribute.Name;
            }

            //bool ddl
            else if (ConstDataType.BOOLEAN.Equals(attribute.getP_Colmun().Type))
            {
                control_id = "ddl_" + attribute.Name;
            }

            //Img
            else if (ConstDataType.IMAGE.Equals(attribute.getP_Colmun().Type))
            {
                control_id = "txt_" + attribute.Name;
            }

            //File
            else if (ConstDataType.FILE.Equals(attribute.getP_Colmun().Type))
            {
                control_id = "txt_" + attribute.Name;
            }

            //Date
            else if (ConstDataType.DATE.Equals(attribute.getP_Colmun().Type))
            {
                control_id = "txt_" + attribute.Name;
            }

            else
            {
                control_id = "txt_" + attribute.Name;
            }
            Control control = this.FindControl(control_id);
            string value = request[control_id];
            if (CDEConstants.IsEmpty(value))
            {
                if (control != null)
                {
                    if (Convert.ToString(control).Trim().EndsWith("DropDownList"))
                    {
                        DropDownList ddl = (DropDownList)control;

                        string canzhao = ddl.Attributes["CanZhao"];

                        if (canzhao != "" && canzhao != null)
                        {

                            CDEAttribute s_attribute = attribute.getSAttribute();
                            entity.set(attribute, null);
                            while (s_attribute.getSAttribute() != null)
                            {
                                entity.set(s_attribute, null);
                                s_attribute = s_attribute.getSAttribute();
                            }
                            entity.set(s_attribute, value);
                        }
                        else
                        {
                            entity.set(attribute, null);
                        }
                    }
                    else if (Convert.ToString(control).Trim().EndsWith("CheckBox"))
                    {
                        entity.set(attribute, false);
                    }
                    else
                    {
                        entity.set(attribute, null);
                    }
                }
            }
            else
            {
                if (CDEConstants.ConstPointType_P_TARGET.Equals(attribute.P_type))
                {
                    if (control_id.StartsWith("ddl_"))
                    {

                        CDEAttribute s_attribute = attribute.getSAttribute();
                        entity.set(attribute, null);
                        while (s_attribute.getSAttribute() != null)
                        {
                            entity.set(s_attribute, null);
                            s_attribute = s_attribute.getSAttribute();
                        }
                        entity.set(s_attribute, value);

                    }
                    else
                    {
                        entity.set(attribute, value);
                    }
                }
                else if (CDEConstants.ConstDispType_DISP.Equals(attribute.P_type))
                {

                    if (value != null && Convert.ToString(control).Trim().EndsWith("CheckBox"))
                    {

                        entity.set(attribute, true);
                    }
                    else
                    {
                        entity.set(attribute, value);
                    }
                }
                else
                {
                    entity.set(attribute, value);
                }
            }
        }

        HiddenField checkId = (HiddenField)this.FindControl("hidCheckId");
        if (checkId != null)
        {
            if (CDEConstants.IsEmpty(checkId.Value))
            {
                HiddenField selectId = (HiddenField)this.FindControl("hidSelectId");
                if (selectId != null)
                {
                    entity.Id = selectId.Value;
                }
            }
            else
            {
                entity.Id = checkId.Value;
            }
        }
    }
}
